package org.jeecg.modules.iot.mqtt.server.service.processor.carOperate;

import org.jeecg.modules.iot.jtt808.JTT808Encoder;
import org.jeecg.modules.iot.jtt808.JTT808Message;
import org.jeecg.modules.iot.mqtt.entity.Packet;
import org.jeecg.modules.iot.model.Jt808Control.Jt808CentralControlConfig;
import org.jeecg.modules.iot.model.modbus.ModbusParams;
import org.jeecg.modules.iot.mqtt.server.service.processor.JT808MessageGenerator;
import org.jeecg.modules.iot.mqtt.server.service.processor.MessageType;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;

import static org.jeecg.modules.iot.utils.JT808Utils.bytesToHexString;

@Component
public class TerminalControlMessageGenerator implements JT808MessageGenerator {

    @Override
    public String generateMessage(JTT808Message message) {
        return "";
    }



    @Override
    public String generateMessage(JTT808Message message, ModbusParams params) {
        return "";
    }

    @Override
    public String getType() {
        return MessageType.PLAY_AUDIO.name();
    }

    // 生成终端控制指令
    private String generateMessage(JTT808Message message, byte commandWord, String commandParams) {
        message.id = 0x8105; // 终端控制消息 ID
        message.packetCount = 0;
        message.packetIndex = 0;

        // 将命令参数字符串转换为 GBK 编码的字节数组
        byte[] commandParamsBytes = commandParams.getBytes(Charset.forName("GBK"));

        // 创建 Packet 对象
        Packet p = Packet.create(1 + commandParamsBytes.length);
        p.addByte(commandWord); // 添加命令字
        p.addBytes(commandParamsBytes); // 添加命令参数

        message.body = p.getBytes();

        return bytesToHexString(JTT808Encoder.encode(message));
    }

    // 遥控器配对 (0xB0)
    public String generateRemoteControlPairMessage(JTT808Message message) {
        return generateMessage(message, (byte) 0x01, String.valueOf((byte) 0xB0));
    }
    @Override
    public String generateMessage(JTT808Message message, Jt808CentralControlConfig config) {
        if (MessageType.PLAY_AUDIO.name().equals(getType())){ // 检查消息类型是否为 PLAY_AUDIO
            message.id = 0x8105; // 终端控制消息 ID
            message.packetCount = 0;
            message.packetIndex = 0;

            // 创建 Packet 对象
            Packet p = Packet.create(2); // 命令字 (1 字节) + 音频索引 (1 字节)
            p.addByte((byte) 0xB3); // 命令字: 指定音频播放 (0xB3)
            p.addByte(  config.getAudioIndex()); // 音频索引

            message.body = p.getBytes();

            return bytesToHexString(JTT808Encoder.encode(message));
        }
        return "";
    }
    // 清空遥控器 (0xB1)
    public String generateClearRemoteControlMessage(JTT808Message message) {
        return generateMessage(message, (byte) 0x01, String.valueOf((byte) 0xB1));
    }

    // 清空蓝牙配对信息 (0xB2)
    public String generateClearBluetoothPairingMessage(JTT808Message message) {
        return generateMessage(message, (byte) 0x01, String.valueOf((byte) 0xB2));
    }

    // 指定音频播放 (0xB3)
    public String generatePlayAudioMessage(JTT808Message message, int audioIndex) {
        if (audioIndex < 0 || audioIndex > 20) {
            throw new IllegalArgumentException("Invalid audio index: " + audioIndex);
        }
        return generateMessage(message, (byte) 0x01, String.valueOf((byte) 0xB3) + audioIndex);
    }
}